package org.gdpu.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.gdpu.model.Person;
import org.gdpu.model.Resource;
import org.gdpu.model.Valid;
import org.gdpu.service.ResourceService;
import org.gdpu.vo.ResourceVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-4
 * Time: 下午10:13
 * To change this template use File | Settings | File Templates.
 */
@Component("upload")
@Scope("prototype")
public class UploadAction extends MyActionSupport implements ModelDriven<ResourceVO> {

    private ResourceService resourceService;

    private final ResourceVO resourceVO = new ResourceVO();

    private Logger logger = LogManager.getLogger(UploadAction.class.getName());


    @Override
    public String execute() throws Exception {
        HttpSession session = ServletActionContext.getRequest().getSession();
        Person person = (Person) session.getAttribute("person");
        if (person == null) {
            view = "/login.jsp";
            return VIEW;
        }
        if (resourceService.uploadResource(resourceVO)) {
            resourceVO.setResultMessage("上传成功");
            return SUCCESS;
        }
        resourceVO.setResultMessage("上传失败");
        return ERROR;
    }

    //审核上传文件
    public String valid() {
        if ("审核通过".equals(resourceVO.getValid()))
            resourceService.judgeResource(Valid.PASS_VALID, Integer.valueOf(resourceVO.getResourceId()));
        else if ("审核失败".equals(resourceVO.getValid()))
            resourceService.judgeResource(Valid.FAIL_VALID, Integer.valueOf(resourceVO.getResourceId()));
        else {
            resourceVO.setResultMessage("操作失败!");
            return SUCCESS;
        }
        resourceVO.setResultMessage("操作成功!");
        return SUCCESS;
    }

    //查询未审核文件
    public String searchNotValid() {
        List<Resource> resources = resourceService.findByNotValid();
        if (resources == null) {
            resourceVO.setResultMessage("找不到相关资源~");
            return ERROR;
        }
        for (Resource r : resources)
            r.setFileName(r.getFileName());
        resourceVO.setResources(resources);
        view = "/page/Valid/showValidFile.jsp";
        return VIEW;
    }

    //显示某个资源
    public String showOne() {
        Resource resource = resourceService.find(Resource.class, Integer.valueOf(resourceVO.getResourceId()));
        resourceVO.setResource(resource);
        view = "/page/Valid/ValidFile.jsp";
        return VIEW;
    }


    @Override
    public ResourceVO getModel() {
        return resourceVO;
    }

    @javax.annotation.Resource(name = "resourceServiceImpl")
    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }
}
